﻿using System;
using System.Collections.Generic; 
using System.Text;
using ATSModel;
using ATSIDAL;
using System.Data.SqlClient;
using System.Data;

namespace ATSDAL
{
    public class TPackageDal : BaseRepository_DT<TPackageInfo>, ITPackage
    {
        #region
        //private const string PARM_TPACKAGE_ID = "@ID";
        private const string PARM_TPACKAGE_RETROSPECTSN = "@RetrospectSN";
        private const string PARM_TPACKAGE_ANTENNASN = "@ProductSN";
        private const string PARM_TPACKAGE_OUTSIDEBSN = "@OutsideBSN";
        private const string PARM_TPACKAGE_OUTSIDECSN = "@OutsideCSN";
        private const string PARM_TPACKAGE_PRODUCTYPEID = "@ProductTypeID";
        private const string PARM_TPACKAGE_PACKAGETIME = "@PackageTime";
        private const string PARM_TPACKAGE_PACKAGEUSER = "@PackageUser";
        private const string PARM_TPACKAGE_PCIP = "@PCIP";
        private const string PARM_TPACKAGE_REMARK = "@Remark";
        private const string PARM_SN = "@SN"; 

        private const string PARM_TPACKAGE_TBC = "@TBC";
        private const string PARM_TPACKAGE_BTBC = "@BTBC";
        private const string PARM_TPACKAGE_OCSN1 = "@OCSN1";
        private const string PARM_TPACKAGE_OCSN2 = "@OCSN2";
        private const string PARM_TPACKAGE_OCSN3 = "@OCSN3";
        private const string PARM_TPACKAGE_ALDSN = "@ALDSN";
        
        #endregion
      
        /// <summary>
        ///检测追溯条码 
        /// </summary>
        /// <param name="retrospectSN">追溯码</param>
        /// <returns>TPackageInfo</returns>
        public TPackageInfo GetRetrospectSN(string retrospectSN)
        {
            SqlParameter parm = GetParameter(PARM_TPACKAGE_RETROSPECTSN); 
            parm.Value = retrospectSN;
            return ExcuteSqlToGetEntity("sp_selectbyRetrospectSN", parm);
        }
        
        /// <summary>
        /// 检测天线条码
        /// </summary>
        /// <param name="antennaSN">天线条码</param>
        /// <returns>TPackageInfo</returns>
        public TPackageInfo GetAntennaSN(string antennaSN)
        {
            SqlParameter parm = GetParameter(PARM_TPACKAGE_ANTENNASN);
            parm.Value = antennaSN;
            return ExcuteSqlToGetEntity("sp_selectbyAntennaSN", parm);
        }

       
        /// <summary>
        /// 根据条码检索
        /// </summary>
        /// <param name="SN">追溯码 或者天线码</param>
        /// <returns>TPackageInfo</returns>
        public IList<TPackageInfo> GetPackInfoBySN(string SN)
        {
            SqlParameter parm = GetParameter(PARM_SN);
            parm.Value = SN;
            return ExcuteSqlToGetEntities("sp_selPackRecordBySN", parm);
        }


        /// <summary>
        /// 根据时间检索包装记录
        /// </summary>
        /// <param name="starttime">开始时间</param>
        /// <param name="stoptime">结束时间</param>
        /// <returns>IList<TPackageInfo></returns>
        public IList<TPackageInfo> GetPackInfobyDT(DateTime starttime, DateTime stoptime)
        {
            SqlParameter[] prParms = GetDTParameters();
            prParms[0].Value = starttime;
            prParms[1].Value = stoptime;
            return ExcuteSqlToGetEntities("sp_selPackRecordByDate", prParms);
        }

        /// <summary>
        /// 根据产品类型检索包装记录
        /// </summary>
        /// <param name="PType"></param>
        /// <returns></returns>
        public IList<TPackageInfo> GetPackInfoByPType(string PType)
        {
            SqlParameter parm = GetParameter(PARM_TPACKAGE_PRODUCTYPEID);
            parm.Value = PType;
            return ExcuteSqlToGetEntities("sp_selectbyPTypeSN", parm);
        }


        /// <summary>
        /// 根据产品序列号 检索产品是否包装
        /// </summary>
        /// <param name="productSN">天线条码</param>
        /// <returns>TPackageInfo</returns>
        bool i;
        public TPackageInfo ProductSN_IsPackage(string productSN)
        {
            i = true;
            SqlParameter parm = GetParameter(PARM_TPACKAGE_ANTENNASN);
            parm.Value = productSN;
            return ExcuteSqlToGetEntity("sp_selPackRecordByProductSN", parm);
        }

        public IList<TPackageInfo> IsPackageBySN(string productSN) 
        { 
            i = true;
            SqlParameter parm = GetParameter(PARM_TPACKAGE_ANTENNASN);
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("sp_selPackRecordByProductSN", parm);
        } 
         
        private static SqlParameter GetParameter(string parmname)
        {
            SqlParameter parm = new SqlParameter(parmname, SqlDbType.NVarChar, 50);
            return parm;
        }

        #region 重写基础方法
        public override TPackageInfo GetEntityRecord(SqlDataReader rdr)
        {
            if (i != true)
            {
                TPackageInfo pac = new TPackageInfo
                {
                    //ID =Convert.ToInt32(rdr.GetValue(0)),
                    RetrospectSN = rdr.GetValue(0).ToString(),
                    ProductSN = rdr.GetValue(1).ToString(),
                    OutsideBSN = rdr.GetValue(2).ToString(),
                    OutsideCSN = rdr.GetValue(3).ToString(),
                    ProductTypeID = rdr.GetValue(4).ToString(),
                    PackageTime = Convert.ToDateTime(rdr.GetValue(5)),
                    PackageUser = rdr.GetValue(6).ToString(),
                    PCIP = rdr.GetValue(7).ToString(),
                    Remark = rdr.GetValue(8).ToString(),
                    TBC = rdr.GetValue(9) is DBNull ? null : rdr.GetValue(9).ToString(),
                    BTBC = rdr.GetValue(10) is DBNull ? null : rdr.GetValue(10).ToString(),
                    OCSN1 = rdr.GetValue(11) is DBNull ? null : rdr.GetValue(11).ToString(),
                    OCSN2 = rdr.GetValue(12) is DBNull ? null : rdr.GetValue(12).ToString(),
                    OCSN3 = rdr.GetValue(13) is DBNull ? null : rdr.GetValue(13).ToString(),
                    ALDSN = rdr.GetValue(14) is DBNull ? null : rdr.GetValue(14).ToString()

                };
                return pac;
            }
            else 
            {
                TPackageInfo pac = new TPackageInfo
                {
                    RetrospectSN = rdr.GetValue(0).ToString(),
                    ProductSN = rdr.GetValue(1).ToString(),
                    OutsideBSN = rdr.GetValue(2).ToString(),
                    OutsideCSN = rdr.GetValue(3).ToString(),
                    ProductTypeID = rdr.GetValue(4).ToString(),
                    PackageTime = Convert.ToDateTime(rdr.GetValue(5)),
                    PackageUser = rdr.GetValue(6).ToString(),
                    PCIP = rdr.GetValue(7).ToString(),
                    Remark = rdr.GetValue(8).ToString(),
                    TBC = rdr.GetValue(9) is DBNull ? null : rdr.GetValue(9).ToString(),
                    BTBC = rdr.GetValue(10) is DBNull ? null : rdr.GetValue(10).ToString(),
                    OCSN1 = rdr.GetValue(11) is DBNull ? null : rdr.GetValue(11).ToString(),
                    OCSN2 = rdr.GetValue(12) is DBNull ? null : rdr.GetValue(12).ToString(),
                    OCSN3 = rdr.GetValue(13) is DBNull ? null : rdr.GetValue(13).ToString(),
                    ALDSN = rdr.GetValue(14) is DBNull ? null : rdr.GetValue(14).ToString()
                };
                return pac;
            }
        }

        public override TPackageInfo GetEntityRecord()
        {
            TPackageInfo pac = new TPackageInfo();
            return pac;
        }

        public override string GetInsertsp()
        {
            return "sp_InsPackRecordInfo";
        }

        public override string GetUpdatesp()
        {
            return "sp_upPackRecordInfo";    //存储过程未实现
        } 

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    //new SqlParameter(PARM_TPACKAGE_ID, SqlDbType.Int),
					new SqlParameter(PARM_TPACKAGE_RETROSPECTSN, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TPACKAGE_ANTENNASN, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TPACKAGE_OUTSIDEBSN, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TPACKAGE_OUTSIDECSN, SqlDbType.NVarChar, 50) ,
                    new SqlParameter(PARM_TPACKAGE_PRODUCTYPEID, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TPACKAGE_PACKAGETIME, SqlDbType.DateTime) ,
                    new SqlParameter(PARM_TPACKAGE_PACKAGEUSER, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TPACKAGE_PCIP, SqlDbType.NVarChar, 20), 
                    new SqlParameter(PARM_TPACKAGE_REMARK, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TPACKAGE_TBC,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TPACKAGE_BTBC,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TPACKAGE_OCSN1,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TPACKAGE_OCSN2,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TPACKAGE_OCSN3,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TPACKAGE_ALDSN,SqlDbType.NVarChar,50)
                };
            return parms;
        }

        public override void SetParameters(TPackageInfo package, params SqlParameter[] Parms)
        { 
            //Parms[0].Value = package.ID;
            Parms[0].Value = package.RetrospectSN;
            Parms[1].Value = package.ProductSN;
            Parms[2].Value = package.OutsideBSN;
            Parms[3].Value = package.OutsideCSN;
            Parms[4].Value = package.ProductTypeID;
            Parms[5].Value = package.PackageTime;
            Parms[6].Value = package.PackageUser;
            Parms[7].Value = package.PCIP;
            Parms[8].Value = package.Remark;
            Parms[9].Value = package.TBC;
            Parms[10].Value = package.BTBC;
            Parms[11].Value = package.OCSN1;
            Parms[12].Value = package.OCSN2;
            Parms[13].Value = package.OCSN3;
            Parms[14].Value = package.ALDSN;
        }
        #endregion
        #region IBaseRepository<TPackageInfo> 成员

        public IList<TPackageInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("sp_selPackRecord");  //存储未实现
        }
        public TPackageInfo LoadEntity<T1>(T1 Id)
        {
            throw new NotImplementedException();
        }

        #endregion
        
    }
}
